//
//  WithDrawViewController.m
//  lhpc_manager
//
//  Created by 李英杰 on 2017/9/6.
//  Copyright © 2017年 zhaoyongjie. All rights reserved.
//

#import "WithDrawViewController.h"
#import "WithDrawView.h"
#import "PassWordView.h"
#import "KLCPopup.h"
#import "VerifyPhoneController.h"
#import "LHNetworking.h"

@interface WithDrawViewController ()<WithDrawViewDelegate>

@property (nonatomic, strong) WithDrawView * mainView;

@property (nonatomic, strong) KLCPopup * popup;

@end

@implementation WithDrawViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.mainView.wxTextField becomeFirstResponder];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = BACKCOLOR;
    self.navigationItem.title = @"提现";
    
    [self.view addSubview:self.mainView];
    
    [self.mainView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.equalTo(self.view);
        
    }];
    
    
    
    [self initData];
}

-(void)initData{
    
    self.mainView.count = self.withDrawCount;
    
    NSString * type = [HLSharedUserDefault objectForKey:@"withDrawType"];
    NSString * account = [HLSharedUserDefault objectForKey:@"withDrawAccount"];
    
    if ([type isEqualToString:@"ali"]) {
        //支付宝
        [self.mainView setDefaultStyle:LHPayTypeAliPay AndAccount:account];
    }else if ([type isEqualToString:@"wx"]){
        //微信
        [self.mainView setDefaultStyle:LHPayTypeWXPay AndAccount:account];
    }
    
    
}

-(void)withDrawButtonHasClick:(NSString *)withDrawAccount{
    
    
    PassWordView * view = [[PassWordView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 200)];
    view.title = @"请输入密码";
    
    LHPC_WEAK_SELF;
    view.completeInPutPassWord = ^(NSString *passWordStr) {
        LHPC_STRONG_SELF;
        [strongSelf requestChangeToMoney:passWordStr];
        
    };
    
    view.abordInPutPassWord = ^{
        
        [self.popup dismiss:YES];
        
    };
    
    view.frogetPassWordBlock = ^{
        [self.popup dismiss:YES];
        VerifyPhoneController * view = (VerifyPhoneController *)LHGetSB_VC(@"VerifyPhone");
        [self.navigationController pushViewController:view animated:YES];
        
    };
    
    self.popup = [KLCPopup popupWithContentView:view showType:KLCPopupShowTypeSlideInFromBottom dismissType:KLCPopupDismissTypeNone maskType:KLCPopupMaskTypeDimmed dismissOnBackgroundTouch:YES dismissOnContentTouch:NO];
    
    [self.popup showAtCenter:CGPointMake(kScreenW/2, (kScreenH - 216 - 164)) inView:self.view];
    
    
}


/**
 提现

 @param password 密码
 */
- (void)requestChangeToMoney:(NSString *)password {
    
    NSString *account = [self.mainView getAccountText];
    
    
    NSString * type = self.mainView.isWXOrAli == 0 ? @"ali" : @"wx";
    
    [HLSharedUserDefault setObject:account forKey:@"withDrawAccount"];
    [HLSharedUserDefault setObject:type forKey:@"withDrawType"];
    [HLSharedUserDefault synchronize];
    
    [HLProgressHUD showLodingHudWithText:nil view:self.view];
    LHPC_WEAK_SELF;
    [[LHNetworking shared] POST:LHSetRequestAPI(LHWithDrawConfirm) parameters:@{@"password" : [password hl_md5], @"account" : account, @"type" : [NSString stringWithFormat:@"%ld",(long)self.mainView.isWXOrAli], @"cash" : [NSString stringWithFormat:@"%.2f",self.withDrawCount]} success:^(NSURLSessionDataTask *task, id responseObject) {
        LHPC_STRONG_SELF;
        [HLProgressHUD dismissMBHud:strongSelf.view];
        if (responseObject) {
            [strongSelf.popup dismiss:YES];
            [HLNoteCenter postNotificationName:LHReloadUnCompleteHome object:nil];
            [HLProgressHUD showHUDWithStyle:HLProgressHUDStyleSuccess status:@"申请成功"];
            [strongSelf.navigationController popViewControllerAnimated:YES];
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        LHPC_STRONG_SELF;
        [HLProgressHUD dismissMBHud:strongSelf.view];
    }];
}

#pragma mark - getter

-(WithDrawView *)mainView{
    if (!_mainView) {
        _mainView = [[WithDrawView alloc]init];
        _mainView.delegate = self;
    }
    return _mainView;
}


@end
